<?php

require_once(LIB_PATH.'/libRVT.php');

$rvt = new RVT();

$images_ext = array('jpg','gif','png','bmp');

function findImages( $fl, $alloc ){
	global $images_ext;
	$res=array();
	
	if (is_array($fl) && count($fl) > 0){
		// Directories
		foreach( $fl['d'] as $d=>$v )
			$res = array_merge(findImages(&$v['content'],$alloc),$res);
	
		// Files
		foreach( $fl['f'] as $file=>$inode )
			foreach($images_ext as $ext)
				if (preg_match('/\.'.$ext.'$/i',$file) && !preg_match('/^\.?_.*/',$file))
					$res[]=array('name'=>$file,'inode'=>$inode,'alloc'=>$alloc);
	}
	
	return $res;
}

function getImages( $base , $from, $to){
	global $rvt;
	
	$base = $rvt->arrayExplode(explode('+',$base));
	$images = array();
	foreach ($base[$_SESSION['case']] as $dev=>$disks)
		foreach ($disks as $disk=>$parts)
			foreach ($parts as $part=>$p){
				$fl = $rvt->getFileList($part);
				if (! empty($fl))
					$images[$part] = array_merge(findImages(&$fl['Allocated'],'Allocated'),findImages(&$fl['Deleted'],'Deleted'));
			}
	
	return $images;
}

if ( isset($_GET['action'])){
	$action = $_GET['action'];
	$base = $_GET['base'];
	switch ($action){
		case "init":
			$images = getImages($base);
			if (count($images) == 0) print "No images found";
			else {
				$smarty->assign('base',$base);
				$smarty->assign('images',$images);
				$smarty->assign('init',1);
				$smarty->display($_GET['plugin'].'.tpl');
			}
			break;
		case "info":
			if (isset($_GET['inode']) && isset($_GET['part'])){
				$part = $rvt->getPartition($_GET['part']);
				$p = split("-",$_GET['part']);
				$case = $rvt->getCase($p[0]);
				$path = $conf['morgue_path'].'/images/'.$case['name'].'-'.$case['code'].'/'.$p[0].'-'.$p[1].'-'.$p[2].'.dd';
				$tmpfile = '/tmp/'.$_GET['inode'].'_'.$_GET['part'].'.img';
				$command = 'icat -o '.$part['osects'].' "'.$path.'" '.$_GET['inode'].' > '.$tmpfile;
				`$command`;
				$exif = exif_read_data($tmpfile);
				if ($exif===false){
					echo "No header data found.<br />";
					unlink($tmpfile);
				} else {
					if (isset($exif['THUMBNAIL'])){
						echo '<center><img src="index.php?plugin=imagePlugin&action=thumb&file='.$tmpfile.'"></center><br/>';
					} else {
						unlink($tmpfile);
					}
					foreach ($exif as $key => $section) {
						if (is_array($section))
							echo "<b>$key:</b><br/><ul>";
						foreach ($section as $name => $val) {
							echo "<li><b>$name</b>: $val</li>\n";
						}
						echo "</ul>";
					}
				}
			}
			break;
		case "thumb":
			if (isset($_GET['file'])){
			    $image = exif_thumbnail($_GET['file'], $width, $height, $type);
		    	header('Content-type: ' .image_type_to_mime_type($type));
			    echo $image;
			    unlink($tmpfile);
			}
			break;
		case "getImage":
			if (isset($_GET['inode']) && isset($_GET['part'])){
				$part = $rvt->getPartition($_GET['part']);
				$p = split("-",$_GET['part']);
				$case = $rvt->getCase($p[0]);
				$path = $conf['morgue_path'].'/images/'.$case['name'].'-'.$case['code'].'/'.$p[0].'-'.$p[1].'-'.$p[2].'.dd';
				$command = 'icat -o '.$part['osects'].' "'.$path.'" '.$_GET['inode'].' 2>&1';
				$image=`$command`;
				
				if (isset($_GET['thumb'])){
					$width_d = 120;
					$src = imagecreatefromstring($image);
					if (!$src) exit();
					$width = imagesx($src);
			    	$height = imagesy($src);
			    	$height_d = floor(($width_d*$height) / $width);
			        $dest = imagecreatetruecolor($width_d, $height_d);
			        // desactivo el procesamiento automatico de alpha
//			        imagealphablending($dest, false);
			        // hago que el alpha original se grabe en el archivo destino
//			        imagesavealpha($dest, true);
			        imagecopyresampled($dest, $src, 0, 0, 0, 0, $width_d, $height_d, $width, $height);
			        header('Content-type: image/jpeg');
					imagejpeg($dest);
			        imagedestroy($im);
				} else {
					print $image;
				}
			}
			break;

	}
}

?>
